﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace Cid.Models
{
	public partial class ConferenceEvent : Base<ConferenceEvent>, IConferenceEvent 
	{

		public override object UniqueId
		{
			get
			{
				return this.Id;
			}
		}

		new protected static DataContext CreateContext()
		{
			return new CidDataContext();
		}

		protected override void Initialize()
		{
			this._Id = Guid.NewGuid();
			this._Description = "";
			this._Name = "";
			this._Location = "";
			this._MetaData = "";
			this._Summary = "";
			this._Track = "";
			this._StartsAt = DateTime.Now;
			this._EndsAt = DateTime.Now.AddHours(1);
			this.labels = new EntitySet<Label>(new Action<Label>(AddLabel), new Action<Label>(RemoveLabel));
			base.Initialize();
		}

		public static IEnumerable<ConferenceEvent> FindByRelevantTime(int hourSpan)
		{
			return Find("SELECT * FROM ConferenceEvents WHERE StartsAt >= {0} AND StartsAt <= {1} ORDER BY StartsAt", DateTime.Now, DateTime.Now.AddHours(hourSpan));
		}

		public override void Detach()
		{
			this.PropertyChanged = null;
			this.PropertyChanging = null;
			base.Detach(); 
		}

		#region IConferenceEvent Members

		protected override void StaticAddRules()
		{
			base.StaticAddRules();
			//this.AddRule(new[] { "Name", "Summary" },
			//	new Validation.ValidatePresenceRule());

			//this.AddRule(new[] { "StartsAt", "EndsAt" },
			//    new Validation.ValidateFormatRule()
			//    {
			//        With = @"^(?=\d)(?:(?:31(?!.(?:0?[2469]|11))|(?:30|29)(?!.0?2)|29(?=.0?2.(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\x20|$))|(?:2[0-8]|1\d|0?[1-9]))([-./])(?:1[012]|0?[1-9])\1(?:1[6-9]|[2-9]\d)?\d\d(?:(?=\x20\d)\x20|$))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$",
			//        Message = "Must be in a valid date format"
			//    });
		}



		protected EntitySet<Label> labels;
		public EntitySet<Label> Labels
		{
			get {
				return null;
			}
			set {
				this.labels.Assign(value);
			}
		}

		protected virtual void AddLabel(Label item)
		{
			this.attach_ConferenceEventLabels(new ConferenceEventLabel() { ConferenceEvent = this, Label = item });
		}

		protected virtual void RemoveLabel(Label item)
		{
			ConferenceEventLabel eLabel =	this.ConferenceEventLabels.SingleOrDefault(o => o.Label == item);
			if (eLabel != null)
				this.detach_ConferenceEventLabels(eLabel);
		}



		protected override void OnSave()
		{
			if (this.Labels != null && this.Labels.Count > 0)
			{
				if (this.ConferenceEventLabels.IsDeferred)
				{
					CidDataContext db = (this.GetContext() as CidDataContext);
					this.ConferenceEventLabels.Assign((from o in db.ConferenceEventLabels where o.ConferenceEventId == this.Id select o));
				}
				foreach (Label label in this.Labels)
					if (this.ConferenceEventLabels.SingleOrDefault(o => o.LabelId == label.Id) == null)
						this.ConferenceEventLabels.Add(new ConferenceEventLabel() { Label = label, ConferenceEvent = this });
			}
		}

		IEnumerable<IConferencePresenter> IConferenceEvent.Presenters
		{
			get
			{
				throw new NotImplementedException();
			}
			set
			{
				throw new NotImplementedException();
			}
		}

		IEnumerable<ILabel> IConferenceEvent.Labels
		{
			get
			{
				throw new NotImplementedException();
			}
			set
			{
				throw new NotImplementedException();
			}
		}
		#endregion
	}
}
